﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;

namespace SuperX.Common.Helper
{
    /// <summary>
    /// 名称合法性校验工具类
    /// </summary>
    public class NameValidationUtil
    {
        // 
        public static bool IsValidName(string name)
        {
            // Grammar:
            // <identifier> ::= <identifier_start> ( <identifier_start> | <identifier_extend> )* 
            // <identifier_start> ::= [{Lu}{Ll}{Lt}{Lo}{Nl}('_')]
            // <identifier_extend> ::= [{Mn}{Mc}{Lm}{Nd}]
            if (string.IsNullOrEmpty(name))
            {
                return false;
            }
            name = name.ToLower();
            short num = 0;
            for (int i = 0; i < name.Length; i++)
            {
                UnicodeCategory unicodeCategory = char.GetUnicodeCategory(name[i]);
                if (unicodeCategory == UnicodeCategory.OtherLetter)
                {
                    num += 8;
                }
                else
                {
                    num += 1;
                }
                bool flag = unicodeCategory == UnicodeCategory.UppercaseLetter || unicodeCategory == UnicodeCategory.LowercaseLetter || unicodeCategory == UnicodeCategory.TitlecaseLetter || unicodeCategory == UnicodeCategory.OtherLetter || unicodeCategory == UnicodeCategory.LetterNumber;
                bool flag2 = unicodeCategory == UnicodeCategory.NonSpacingMark || unicodeCategory == UnicodeCategory.SpacingCombiningMark || unicodeCategory == UnicodeCategory.ModifierLetter || unicodeCategory == UnicodeCategory.DecimalDigitNumber || name[i] == '_';
                if (num > 200)
                {
                    return false;
                }
                if (i == 0)
                {
                    if (!flag)
                    {
                        return false;
                    }
                }
                else if (!flag && !flag2)
                {
                    return false;
                }
            }
            return true;
        }

        // 
        public static bool IsKeyword(string name)
        {
            return NameValidationUtil.Keywords.Contains(name);
        }

        /// <summary>
        /// 验证密码是否符合要求
        /// </summary>
        /// <param name="password"></param>
        /// <param name="minLength"></param>
        /// <returns></returns>
        public static bool IsValidPassword(string password, int minLength = 0)
        {
            if (string.IsNullOrEmpty(password) || password.Length < minLength)
            {
                return false;
            }
            string text = (minLength <= 0) ? "^[A-Za-z0-9./\\|_@\\-\\+#? :]+$" : string.Format("^[A-Za-z0-9./\\|_@\\-\\+#? :]{{{0},}}$", minLength);
            return Regex.IsMatch(password, text);
        }

        public static List<string> Keywords
        {
            get => _keywords ?? (_keywords = new List<string>() { "IO", "Tag", "Alarm" });
        }
        private static List<string> _keywords;
    }
}
